<?php


namespace App\Http\Controllers;


use App\CodeResponse;

class WxController extends Controller
{

    private function codeReturn($codeResponse, $data = null, $info = '')
    {
        list($errno, $errmsg) = $codeResponse;
        $res = ['code' => $errno];
        if (is_array($data)) {
            $data        = array_filter($data, function ($item) {
                return $item !== null;
            });
            $res['data'] = $data;
        } elseif (!is_null($data)) {
            $res['data'] = $data;
        }
        $res['msg'] = $info ?: $errmsg;
        return response()->json($res);
    }


    public function success($data = '', $info = '')
    {
        if (empty($data)){
            $data = (object)[];
        }else{
            $data = $data;
        }
        return $this->codeReturn(CodeResponse::SUCCESS, $data, $info);
    }

    public function fail(array $codedResponse, $info = '', $data = null)
    {
        return $this->codeReturn($codedResponse, $data, $info);
    }

}
